package com.cunyme;

import java.io.IOException;
import java.util.Vector;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class News extends Activity {
	/** Called when the activity is first created. */
	private Vector<String> headlines = new Vector<String>();
	private Vector<String> urls = new Vector<String>();

	private ScrollView sv;
	private LinearLayout ll;

	

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);

		try {

			Connection getPage = Jsoup
					.connect("http://www1.cuny.edu/mu/forum/feed/");
			Document page = getPage.get();

			Elements titles = page.getElementsByTag("title");
			Elements links = page.getElementsByTag("comments");

			// opened database
			DBHelper dbhelper = new DBHelper(this);
			SQLiteDatabase db_news = dbhelper.getWritableDatabase();
			
			db_news.execSQL("DELETE FROM \"news\"");
			
			
			for (int i = 1; titles.size() > i ; i++) {
				String ttls = titles.get(i).text();
				String lks = links.get(i-1).text();
				headlines.add(ttls);	
				urls.add(lks);
				db_news.execSQL("INSERT INTO \"news\" (title, link) VALUES (\"" + ttls + "\", \"" + lks + "\");");
			}
			// close database
			db_news.close();
			dbhelper.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			// If the connection fails, we end up here.
			Toast.makeText(getApplicationContext(),
					"Unable to Establish Connection.\nNews Might be out of date!", Toast.LENGTH_LONG)
					.show();
			e.printStackTrace();
			
			//open database 
			DBHelper dbhelper = new DBHelper(this);
			SQLiteDatabase db_news = dbhelper.getReadableDatabase();
			
			String[] columns = {"title","link"};
			headlines.clear();
			urls.clear();
			
			Cursor db_cur = db_news.query("news", columns, null	, null, null, null, null);
			db_cur.moveToFirst();
			
			int count = 0;
			
			while (count < (db_cur.getCount())){
				headlines.add(db_cur.getString(0));
				urls.add(db_cur.getString(1));
				count++;
				db_cur.moveToNext();
			}
			// Close cursor 
			db_cur.close();
			// close database 
			db_news.close();
			dbhelper.close();

			
			
			

		}

		// display
		sv = new ScrollView(this);
		ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		
		sv.addView(ll);

		for (int i = 0; i < headlines.size(); i++) {
			final TextView tv = new TextView(this);
			tv.setText("-------------\n" + headlines.get(i));
			tv.setHint(urls.get(i));
			tv.setPadding(5, 5, 5, 5);
			tv.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					// TODO Auto-generated method stub					
					String url = (String)tv.getHint();
					Uri uri = Uri.parse(url);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				}
				
			});
			ll.addView(tv);
		}

		setContentView(sv);
	}

}
